package com.example.zyfypt516wh.model;

import com.example.zyfypt516wh.service.CollectionService;
import com.example.zyfypt516wh.bean.CollectResult;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.CollectionIface;
import com.example.zyfypt516wh.iface.CollectionListener;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CollectionModel implements CollectionIface {
    private Retrofit retrofit;
    public CollectionModel() {
        //使用 Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL).addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void exist(String mod, int resid, String sessionID, CollectionListener listener) {

        //使用 Retrofit----2
        CollectionService service=retrofit.create(CollectionService.class);
        Call<String> call
                =service.exists(mod,resid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }

    @Override
    public void collect(String mod, int resid, String sessionID, CollectionListener listener) {

        //使用 Retrofit----2
        CollectionService service=retrofit.create(CollectionService.class);
        Call<String> call =service.collect(mod,resid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }

    @Override
    public void uncollect(String mod, int resid, String sessionID, CollectionListener listener) {

        //使用 Retrofit----2
        CollectionService service=retrofit.create(CollectionService.class);
        Call<String> call =service.uncollect(mod,resid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void CollectionList(String mod, int page, String sessionID, CollectionListener listener) {
        //使用 Retrofit----2
        CollectionService service=retrofit.create(CollectionService.class);
        Call<List<CollectResult>> call =service.getCollectList(mod,page,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<List<CollectResult>>() {
            @Override
            public void onResponse(Call<List<CollectResult>> call, Response<List<CollectResult>> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse( response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<List<CollectResult>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
